//
//  CoolAlertView.m
//  CoolFramework
//
//  Created by Felix on 13-3-31.
//  Copyright (c) 2013年 Xue Fei. All rights reserved.
//

#import "CoolAlertView.h"

@implementation CoolAlertView

+ (void)showAlertWithTitle:(NSString*)title message:(NSString*)message {
    if (title.length == 0 && message.length == 0) {
        return;
    }
    
    UIViewController *rootCtr = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIAlertController *alertCtr = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionDefault = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alertCtr addAction:actionDefault];
    [rootCtr presentViewController:alertCtr animated:YES completion:nil];
    
}

+ (void)showTopAlertWithTitle:(NSString*)title message:(NSString*)message image:(NSString *)image clickAlertBlock:(void(^)(NSInteger tap))clickBlock;
{
    if (title.length == 0 && message.length == 0) {
        return;
    }
    TopAlertView *alertView =[TopAlertView showTopAlertWithTitle:title message:message image:image superView:[UIApplication sharedApplication].keyWindow clickAlertBlock:clickBlock];
    alertView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.15].CGColor;
    alertView.layer.shadowOffset = CGSizeMake(0,0);
    alertView.layer.shadowOpacity = 1;
    alertView.layer.shadowRadius = 19;
    alertView.layer.cornerRadius = 4.0f;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end
@interface TopAlertView (){
    
}
@property (nonatomic, strong)UIImageView *picImageView;
@property (nonatomic, strong)UILabel *contentLabel;
@property (nonatomic, strong)UILabel *titleLabel;
@end

@implementation TopAlertView

+ (instancetype)sharedInstance {
    static TopAlertView *sSharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sSharedInstance = [[self alloc] init];
    });
    return sSharedInstance;
}

+ (TopAlertView *)showTopAlertWithTitle:(NSString*)title message:(NSString*)message image:(NSString *)image superView:(UIView *)superView clickAlertBlock:(void(^)(NSInteger tap))clickBlock
{
    return [[self sharedInstance]initWithTitle:title message:message image:image superView:superView clickAlertBlock:clickBlock];
}

- (TopAlertView *)initWithTitle:(NSString*)title message:(NSString*)message image:(NSString *)image superView:(UIView *)superView clickAlertBlock:(void(^)(NSInteger tap))clickBlock
{
    if (self = [super init]) {
        self.titleNst = title;
        self.messageNst = message;
        self.imageNst = image;
        self.superView = superView;
        self.clickSelf = clickBlock;
        [self initUI];
        [self showView];
    }
    return self;
}

#pragma mark - 初始化UI
- (void)initUI{
    
    [self.superView addSubview:self];
    self.backgroundColor = [UIColor whiteColor];
    self.frame = CGRectMake(16, - STATUSBAR_HEIGHT - 103, WINDOW_WIDTH - 16*2, 103);
    if (!self.picImageView) {
        self.picImageView = [[UIImageView alloc]init];
        [self addSubview:self.picImageView];
    }
   [self.picImageView sd_setImageWithURL:[NSURL URLWithString:self.imageNst]];
    CGFloat selfWidth = WINDOW_WIDTH - 16*2;
    if (!self.titleLabel) {
         self.titleLabel = [CoolUtil creatNormalLabel:[UIColor colorWithHexString:DARK_GRAY_COLOR] align:NSTextAlignmentLeft font:[SCAppAppearance boldFontWithSize:15.0f] parent:self];
    }
   
    self.titleLabel.text = self.titleNst;
    if (!self.contentLabel) {
         self.contentLabel = [CoolUtil creatNormalLabel:[UIColor colorWithHexString:LIGHT_GRAY_COLOR] align:NSTextAlignmentLeft font:[SCAppAppearance defaultContentFontWithSize:12.0f] parent:self];
        self.contentLabel.numberOfLines = 2;
    }
   
    self.contentLabel.text = self.messageNst;
    
    if (self.imageNst.length > 0) {
        self.picImageView.frame = CGRectMake(17, 20, 85, 64);
        self.titleLabel.frame = CGRectMake(self.picImageView.frame.origin.x + self.picImageView.frame.size.width + 10, 25, selfWidth - (self.picImageView.frame.origin.x + self.picImageView.frame.size.width + 10 +17+7), 15);
        self.contentLabel.frame = CGRectMake(self.picImageView.frame.origin.x + self.picImageView.frame.size.width + 10, self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 10, selfWidth - (self.picImageView.frame.origin.x + self.picImageView.frame.size.width + 10 +25), 32);
        
    }else{
        self.titleLabel.frame = CGRectMake(17, 25, selfWidth - (17 +17+7), 15);
        self.contentLabel.frame = CGRectMake(17, self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 10, selfWidth - (17 +25), 32);
    }
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickSelfAction)];
    [self addGestureRecognizer:tap];
    
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [self addGestureRecognizer:recognizer];
}

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.1 animations:^{
        self.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)clickSelfAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(16,  - STATUSBAR_HEIGHT - 103, WINDOW_WIDTH - 16*2, 103);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    if (self.clickSelf) {
        self.clickSelf(0);
    }
}

#pragma mark - 初始化动画
- (void)showView{
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(16, STATUSBAR_HEIGHT, WINDOW_WIDTH - 16*2, 103);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.3 animations:^{
                self.frame = CGRectMake(16,  - STATUSBAR_HEIGHT - 103, WINDOW_WIDTH - 16*2, 103);
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
            }];
        });
    }];
    
}


@end
